import axios from 'axios'
import qs from 'qs'
import Store from '../store'
let instance = axios.create()
//添加请求拦截器
instance.interceptors.request.use(config => {
  //从vuex中获取token 如果有，则设置header一起发送请求
  let token = Store.state.token
  if(token){
    config.headers.authorization = token
  }
  return config
})
//添加响应拦截器
instance.interceptors.response.use(response => {
   if(response.data.code == 401){
    //用户token失效
    window.location = '/user/login'
   }else{
    return response
   }
})
const myAxios = {
    /**
     * 用于发送get请求
     * @param {string} url 
     * @param {Object} params 
     */
   get(url,params){
    return instance({
        method:'get',
        url:url,
        params:params
    })
   },
   /**
     * 用于发送post请求
     * @param {string} url 
     * @param {Object} params {参数1，参数2....}
     */
   post(url,params){
    return instance({
        method:'post',
        url:url,
        data:qs.stringify(params) //将params对象转为查询字符串
    })
   },
}
export default myAxios